package com.bz.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bz.common.Codeenum;
import com.bz.common.PageInfo;
import com.bz.common.PageUtil;
import com.bz.common.ResponseDto;
import com.bz.entity.ServiceEntity;
import com.bz.service.IServiceService;

@RestController
@RequestMapping(value = "/admin/service")
public class ServiceController {

    @Autowired
    private IServiceService serviceService;

    @RequestMapping(value = "/list")
    public PageInfo list(@RequestParam(required = false) String title,
                         @RequestParam(required = false) String type,
                         @RequestParam(required = false) Long pageNo) {

        PageInfo page = PageUtil.getPageInfo(pageNo, PageUtil.pageSize);
        LambdaQueryWrapper<ServiceEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(title)) {
            queryWrapper.like(ServiceEntity::getTitle, title);
        }
        if (StringUtils.isNotEmpty(type)) {
            queryWrapper.eq(ServiceEntity::getType, type);
        }
        return serviceService.list(page, queryWrapper);
    }

    @RequestMapping(value = "/add")
    public ResponseDto<String> add(@RequestBody @Valid ServiceEntity data) {
        serviceService.add(data);
        return new ResponseDto<>(Codeenum.SUCCESS.getCode(), "添加成功");

    }

    @RequestMapping(value = "/update")
    public ResponseDto<String> update(@RequestBody @Valid ServiceEntity data) {
        serviceService.update(data);
        return new ResponseDto<>(Codeenum.SUCCESS.getCode(), "修改成功");
    }

    @RequestMapping(value = "/delete")
    public ResponseDto<String> delete(@RequestParam(required = true) Long id) {

        serviceService.delete(id);
        return new ResponseDto<>(Codeenum.SUCCESS.getCode(), "删除成功");
    }
}
